<?php
function reequipment_user_change_company()
{

    if (!xarVarFetch('phase', 'enum:form:create:move', $phase, 'form', XARVAR_NOT_REQUIRED, XARVAR_PREP_FOR_DISPLAY)) return;
    if (!xarVarFetch('maintenance_id', 'int:1:', $maintenance_id, '', XARVAR_NOT_REQUIRED)) return; 
    if (!xarVarFetch('equipment_id', 'int:1:', $equipment_id, '', XARVAR_NOT_REQUIRED)) return;   
    if (!xarVarFetch('item_id', 'int:1:', $item_id, '', XARVAR_NOT_REQUIRED)) return;   
    if (!xarVarFetch('cycles', 'int:1:', $cycles, '', XARVAR_NOT_REQUIRED)) return;  
    if (!xarVarFetch('frequency_id', 'int:1:', $frequency_id, '', XARVAR_NOT_REQUIRED)) return;  
    if (!xarVarFetch('issue', 'str:1:', $issue, '', XARVAR_NOT_REQUIRED)) return;    
    if (!xarVarFetch('resolution', 'str:1:', $resolution, '', XARVAR_NOT_REQUIRED)) return;     
    if (!xarVarFetch('manhours', 'float', $manhours, '', XARVAR_NOT_REQUIRED)) return;       
    if (!xarVarFetch('downtime', 'float', $downtime, '', XARVAR_NOT_REQUIRED)) return;   
    if (!xarVarFetch('active','isset',$active, array(), XARVAR_NOT_REQUIRED)) return;
    if (!xarVarFetch('move_company_id', 'int:1:', $move_company_id, '', XARVAR_NOT_REQUIRED)) return;  
    
    switch(strtolower($phase)) {
        case 'form':
			// We have to clear the company_id session first.
			xarSessionDelVar('company_id');
			if (!xarSecurityCheck('EditEquipment', 1)) return;
			// Can't do this because we will infinite loop.
			//xarModAPIFunc('reequipment', 'user', 'getcompanyid');
			$data = array();
			// We are just returning to main, so no need to set the company id here.
			$uid = xarSessionGetVar('uid');
			$data['companies'] = xarModAPIFunc('reequipment', 'user', 'getrel', array('user_id' => $uid));
			// $data['equipment'] = xarModAPIFunc('reequipment', 'user', 'getequipment', array('company_id' => $company_id));
            // $data['frequency'] = xarModAPIFunc('reequipment', 'user', 'getfrequency'); 
			$data['inventory'] = array();
			$data['overdue'] = array();
			$data['overdue_parts'] = array();
			$data['open_maintenance'] = array();
			foreach ($data['companies'] as $key => $var){
				// var_export($var['company_id']);
				
				$o = xarModAPIFunc('reequipment', 'user', 'getmaintenance', array('over_due' => true, 'date_complete' => true, 'company_id' => $var['company_id']));
				foreach ($o as $open){
					$data['overdue'][]  = $open;
				}
				$op = xarModAPIFunc('reequipment', 'user', 'getorders', array('over_due' => true, 'company_id' => $var['company_id'], 'is_open' => true));
				foreach ($op as $openp){
					$data['overdue_parts'][]  = $openp;
				}
				$om  = xarModAPIFunc('reequipment', 'user', 'getmaintenance', array('date_complete' => true, 'company_id' => $var['company_id'], 'unscheduled' => true));
				foreach ($om as $openm){
					$data['open_maintenance'][]  = $openm;
				}
				$i = xarModAPIFunc('reequipment', 'user', 'getitems', array('company_id' => $var['company_id'], 'inventory' => true));
				foreach ($i as $inv){
					$data['inventory'][] = $inv;
				}
			}

			// return;
			// return var_export($data['inventory']);
		break;
		
		case 'create':
		
			foreach ($active as $var) {
				$postinfo = xarModAPIFunc('reequipment', 'admin', 'createmaintenance', array('issue' => $issue, 'resolution' => $resolution, 'manhours' => $manhours, 'equipment_id' => $equipment_id, 'company_id' => $var, 'downtime' => $downtime, 'frequency_id' => $frequency_id));
			}

		break;
		
        case 'move':
   				// return var_export($move_company_id);
                $postinfo = xarModAPIFunc('reequipment', 'admin', 'updateitems', array('item_id' => $item_id, 'company_id' => $move_company_id));
                $data['inventory'] = xarModAPIFunc('reequipment', 'user', 'getitems', array('item_id' => $item_id));
                foreach ($data['inventory'] as $var){
                	$data['item_id'] = $var['item_id'];
                	$data['company_id'] = $var['company_id'];
                	$data['company_name'] = $var['company_name'];
                	$data['description'] = $var['description'];
                }
                $uid = xarSessionGetVar('uid');
                $data['companies'] = xarModAPIFunc('reequipment', 'user', 'getrel', array('user_id' => $uid));
                // return var_export($data);
                $template = xarTplModule('reequipment', 'ajax', 'move_inventory', array('item_id' => $data['item_id'], 'company_id' => $data['company_id'],'company_name' => $data['company_name'],'description' => $data['description'], 'companies' =>$data['companies'] ));             
                return $template;
        break; 
		
	}
    // var_export($data['overdue_parts']);
   	return $data;   
}
?>